import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:module_home/pages/home/home_logic.dart';

class HomeDomainWidget extends StatelessWidget {
  HomeLogic logic;

  HomeDomainWidget({super.key, required this.logic});

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Obx(() => GridView.builder(
              padding: EdgeInsets.zero,
              shrinkWrap: true,
              physics: NeverScrollableScrollPhysics(),
              gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                crossAxisCount: 4,
                childAspectRatio: 1.6,
              ),
              itemCount: logic.domainVM.statLength.value,
              itemBuilder: (context, index) {
                final stat = logic.domainVM.statList[index];
                return _statsItem(
                    count: stat.number.toString(),
                    label: stat.title.toString());
              },
            )),
        Obx(() => _buildExpandedButtons(logic)),
      ],
    );
  }

  _statsItem({required String count, required String label}) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        Text(count,
            style: TextStyle(
                fontSize: 16,
                fontWeight: FontWeight.bold,
                color: Colors.white)),
        SizedBox(height: 4),
        Text(
          label,
          style: TextStyle(
              fontSize: 12, fontWeight: FontWeight.w500, color: Colors.white),
        ),
      ],
    );
  }

  _buildExpandedButtons(HomeLogic logic) {
    return GestureDetector(
      onTap: () {
        logic.domainVM.updateExpanded();
      },
      child: AnimatedContainer(
        duration: Duration(milliseconds: 200),
        decoration: BoxDecoration(
          image: DecorationImage(
            image: AssetImage(logic.domainVM.isExpanded.value
                ? 'assets/images/slices/组 70102@3x.png'
                : 'assets/images/slices/组 70112@3x.png'),
            fit: BoxFit.contain,
          ),
        ),
        width: 20,
        height: 20,
      ),
    );
  }
}
